package com.owen.java.demo.io.bio;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author sunger
 * @date 2019/6/21 14:12
 * @Description
 */
public class SocketServer {

    public static Date now = new Date();
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss.SSS");

    public static int i=0;

    public static void main(String[] args) throws IOException {
//        if(args.length!=1){
//            System.out.println("参数不正确");
//            System.exit(1);
//        }
        //为了简单起见，所有的异常信息都往外抛 port = 2005
//        int port = Integer.parseInt(args[0]);
        int port = 2005;
        //定义一个ServerSocket监听在端口9999上
        ServerSocket server = new ServerSocket(port);
        while (true) {
            //server尝试接收其他Socket的连接请求，server的accept方法是阻塞式的
            Socket socket = server.accept();
            Task task1 = new Task(socket);
            Task task2 = new Task(socket);
            //每接收到一个Socket就建立两个个新的线程来处理它
            Thread thread1=new Thread(task1);
            Thread thread2=new Thread(task2);

//            FileOutputStream fileOutputStream1=new FileOutputStream("d:/root/text"+thread1.getName()+".txt");
//            FileOutputStream fileOutputStream2=new FileOutputStream("d:/root/text"+thread2.getName()+".txt");

//            task1.setFileOutputStream(fileOutputStream1);
//            task2.setFileOutputStream(fileOutputStream2);

            thread1.start();
            thread2.start();
        }
    }

    /**
     * 用来处理Socket请求的
     */
    static class Task implements Runnable {

        private Socket socket;
        private FileOutputStream fileOutputStream;

        public Task(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                handleSocket();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public FileOutputStream getFileOutputStream() {
            return fileOutputStream;
        }

        public void setFileOutputStream(FileOutputStream fileOutputStream) {
            this.fileOutputStream = fileOutputStream;
        }

        /**
         * 跟客户端Socket进行通信
         *
         * @throws Exception
         */
        private void handleSocket()  {
           try {
//               Writer writer = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
               DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
//               fileOutputStream=new FileOutputStream("d:\\test.txt");

               String str = "adasdasdada";
               while (socket.isConnected()) {
                   str = getData();
//                   fileOutputStream.write((str+"\r\n").getBytes());
//                   System.out.println(str);
                   dataOutputStream.writeUTF(str);
//                   dataOutputStream.writeUTF(String.valueOf(i++)+"\r\n");
//                   dataOutputStream.writeUTF("aaaaaaaaaaaaaaaa\r\n");
//                   System.out.println(i);
//                   writer.write(str);
                   dataOutputStream.flush();
//                   writer.flush();
                   System.out.println(str);
//                   System.out.println("发送消息中");
//                   Thread.sleep(500L);
               }
               dataOutputStream.close();
//               writer.close();
               socket.close();

           }catch (Exception e){
//               System.out.println("一个连接已断开");
               e.printStackTrace();
           }finally {
               if(null!=fileOutputStream){
                   try {
                       fileOutputStream.close();
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
               }
           }

        }

        public static String getData() {
            return String.valueOf((new Random()).nextInt(27)) + "," + dateFormat.format(now) + "," + getRandom(5) + "," +
                    "" + getRandom(20) + ",1" + getRandom(10) + ",000000000000000" + "" +
                    ",4600" + getRandom(11) + ",1" + getRandom(4) + ",1" + getRandom(4)+"\n";
        }

        public static String getRandom(int a) {
            String res = "";
            for (int i = 0; i < a; i++) {
                res += String.valueOf((new Random()).nextInt(10));
            }

            return res;
        }
    }

}
